package cn.easybuy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductCategoryService;
import cn.easybuy.service.impl.ProductCategoryServiceImpl;

/**
 * 初始化首页信息
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
	
	ProductCategoryService service =null;
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		//初始化几个service
		service = new ProductCategoryServiceImpl();
	} 
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("------销毁前");
		super.destroy();
		System.out.println("------销毁后----");
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("---------------进入了HomeServlet 初始首页信息");
		
		List<ProductCategory> pclist = service.getAllProductCategroy();
		request.setAttribute("pclist", pclist);
		System.out.println("pclist.size: "+ pclist.size());
		
		//页面跳转至首页
		request.getRequestDispatcher("Index.jsp").forward(request, response);
		
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
